﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq;
using System.Collections.ObjectModel;
using eBags.Web.Entity;
using eBags.Web;
using System.Data.SqlClient;

namespace eBags.Web.Controls
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        private const string GetAvailableColorsSp = "GetAvailableColorsForProduct";
        private const string GetAvailableSizesSp = "GetAvailableSizesForProduct";

        private int _productId = 1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this._productId = int.Parse(this.Request.QueryString["id"]);
            

            this.rpColorPicker.DataSource = this.InitPicker<Color>(GetAvailableColorsSp);
            this.rpColorPicker.DataBind();

            this.rpSizePicker.DataSource = this.InitPicker<Size>(GetAvailableSizesSp);
            this.rpSizePicker.DataBind();
        }

        private Collection<T> InitPicker<T>(string spName) where T : IEntity, new()
        {
            Collection<T> collection = new Collection<T>();
            SqlParameter[] parameters = SqlHelperParameterCache.GetSpParameterSet(SqlHelper.ProductConnString, GetAvailableColorsSp);
            parameters[0].Value = this._productId;

            using (SqlDataReader sr = SqlHelper.ExecuteReader(SqlHelper.ProductConnString, CommandType.StoredProcedure, spName, parameters))
            {
                while (sr.Read())
                {
                    T color = new T();
                    
                    color.Id = int.Parse(sr[0].ToString());
                    color.Name = sr[1].ToString();

                    collection.Add(color);
                }
            }

            return collection;
        }
    }
}